package com.ynr.voucher.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.ynr.dao.IBankDao;
import com.ynr.dao.IExpenseDao;
import com.ynr.dao.IInvoiceDao;
import com.ynr.dao.ISalaryDao;
import com.ynr.voucher.dao.IVoucherDao;
import com.ynr.voucher.service.IVoucherService;

@Controller
public class VoucherController {

	private static final Logger logger = LoggerFactory.getLogger(VoucherController.class);
	
	@Autowired
	private IInvoiceDao invoiceDao;
	
	@Autowired
	private IExpenseDao expenseDao;
	
	@Autowired
	private ISalaryDao salaryDao;
	
	@Autowired
	private IBankDao bankDao;
	
	@Autowired
	private IVoucherDao voucherDao;
	
	@RequestMapping("/voucherAudit")
	@ResponseBody
	public Map<String, Object> voucherAudit(HttpServletRequest request) {
		Map<String, Object> retMap = new HashMap<>();
		String companyId = request.getParameter("company_id");
		String billDate = request.getParameter("bill_date");
		logger.debug("VoucherController method auditVoucher, companyId : {}, billDate : {}", companyId, billDate);
		List<Map<String, Object>> dataList = generateDateList(companyId, billDate);
		if(dataList == null || dataList.isEmpty()) {
			retMap.put("error_code", 0);
		} else {
			retMap.put("error_code", 1);
			retMap.put("dataList", JSON.toJSONString(dataList));
		}
		return retMap;
	}
	
	@RequestMapping("/updateVoucherStatus")
	@ResponseBody
	public Map<String, Object> updateVoucherStatus(HttpServletRequest request) {
		Map<String, Object> retMap = new HashMap<>();
		String idsStr = request.getParameter("ids");
		List<String> ids = JSON.parseArray(idsStr, String.class);
		int affectedRows = voucherDao.updateVoucherStatus(ids);
		retMap.put("error_code", affectedRows>0?1:0);
		return retMap;
	}

	@RequestMapping("/getAllCorrectVoucher")
	@ResponseBody
	public Map<String, Object> getAllCorrectVoucher(HttpServletRequest request) {
		String companyId = request.getParameter("company_id");
		String billDate = request.getParameter("bill_date");
		List<Map<String, Object>> voucherList = voucherDao.getVoucherByCompanyIdAndBillDate(companyId, billDate);
		Map<String, Object> retMap = new HashMap<>();
		retMap.put("data", voucherList);
		return retMap;
	}
	
	private List<Map<String, Object>> generateDateList(String companyId, String billDate) {
		List<Map<String, Object>> retList = Lists.newArrayList();
		List<Map<String, Object>> invoiceList = invoiceDao.getInvoiceByCompanyIdAndBillDate(companyId, billDate);
		if(invoiceList != null && !invoiceList.isEmpty()) {
			for(Map<String, Object> invoice : invoiceList) {
				List<Map<String, Object>> itemList = invoiceDao.getItemByInvoiceId((long)invoice.get("invoice_id"));
				List<Map<String, Object>> voucherList = voucherDao.getVoucherByTableNameAndId("invoice", (long)invoice.get("invoice_id"));
				invoice.put("itemList", itemList);
				invoice.put("voucherList", voucherList);
				invoice.put("type", "invoice");
				for(Map<String, Object> voucher : voucherList) {
					if((int)voucher.get("is_correct") == 1) {
						invoice.put("correct", 1);
						break;
					} else {
						invoice.put("correct", 0);
						break;
					}
				}
			}
			retList.addAll(invoiceList);
		}
		List<Map<String, Object>> expenseList = expenseDao.getExpenseByCompanyIdAndBillDate(companyId, billDate);
		if(expenseList != null && !expenseList.isEmpty()) {
			for(Map<String, Object> expense : expenseList) {
				List<Map<String, Object>> voucherList = voucherDao.getVoucherByTableNameAndId("expense", (long)expense.get("id"));
				expense.put("voucherList", voucherList);
				expense.put("type", "expense");
				for(Map<String, Object> voucher : voucherList) {
					if((int)voucher.get("is_correct") == 1) {
						expense.put("correct", 1);
						break;
					} else {
						expense.put("correct", 0);
						break;
					}
				}
			}
			retList.addAll(expenseList);
		}
		List<Map<String, Object>> salaryList = salaryDao.getSalaryByCompanyIdAndBillDate(companyId, billDate);
		if(salaryList != null && !salaryList.isEmpty()) {
			for(Map<String, Object> salary : salaryList) {
				List<Map<String, Object>> voucherList = voucherDao.getVoucherByTableNameAndId("salary_detail", (long)salary.get("id"));
				salary.put("voucherList", voucherList);
				salary.put("type", "salary");
				for(Map<String, Object> voucher : voucherList) {
					if((int)voucher.get("is_correct") == 1) {
						salary.put("correct", 1);
						break;
					} else {
						salary.put("correct", 0);
						break;
					}
				}
			}
			retList.addAll(salaryList);
		}
		List<Map<String, Object>> bankList = bankDao.getBankByCompanyIdAndBillDate(companyId, billDate);
		if(bankList != null && !bankList.isEmpty()) {
			for(Map<String, Object> bank : bankList) {
				List<Map<String, Object>> voucherList = voucherDao.getVoucherByTableNameAndId("bank", (long)bank.get("id"));
				bank.put("voucherList", voucherList);
				bank.put("type", "bank");
				for(Map<String, Object> voucher : voucherList) {
					if((int)voucher.get("is_correct") == 1) {
						bank.put("correct", 1);
						break;
					} else {
						bank.put("correct", 0);
						break;
					}
				}
			}
			retList.addAll(bankList);
		}
		
		return retList;
	}
	
}
